package robotRouting;


import java.awt.event.*;

import javax.swing.JFrame;

import bluetoothcomm.SerialWriter;
import bluetoothcomm.TwoWayComm;

import monitor.AnglesEvent;
import monitor.AnglesEventListener;
import monitor.StopRobotEventListener;
import monitor.dataDrivenController;
import roombacomm.*;
import snd.SndPlayer;
import snd.SndPlayerMbed;

public class FindAPebble extends JFrame implements AnglesEventListener, StopRobotEventListener, KeyListener {
    public static RoombaCommSerial roombacomm;
    public String roombacommPort;
    public static dataDrivenController dataController;
    public static SndPlayer sndplayer;
    
    public static boolean busyAnglesEventListener = false;
    public static boolean busyStopEventListener = false;
    
    public static boolean isRunning = false;
    
	public static TwoWayComm twoWayComm;
//	public static SndPlayerMbed sndplayer;
//	public static SerialWriter roombacomm;
    
    public void initdataDrivenController() {
    	dataController = new dataDrivenController();
    	dataController.initDecoder();
    	dataController.addAnglesEventListener(this);
//    	dataController.addStopRobotEventListener(this);
    }
    
    /**the sec to make the spot search
    */
//    public static void roombaSpot(int sec){
//    	
//		  roombacomm.control();
//        roombacomm.pause(2000);
//        roombacomm.spot();
//        roombacomm.spot();
//        roombacomm.pause(sec*1000);        
//        roombacomm.control();
//        roombacomm.stop();
//    }
    
    public static void roombaSpot(){
		roombacomm.control();
        roombacomm.pause(2000);
        roombacomm.spot();
        roombacomm.spot();
//        roombacomm.pause(sec*1000);        
//        roombacomm.control();
//        roombacomm.stop();
    }
    
    
    public FindAPebble() {
        super();
        setSize( 100, 100 );
        this.addKeyListener(this);
        setVisible(true);
        roombacomm = new RoombaCommSerial();
        roombacomm.debug = true;
        roombacommPort = "/dev/tty.PebbleRobot-SPP";
        while (!connect());
        initialize();
        
//        twoWayComm = new TwoWayComm();
//		   
//		try {
//				twoWayComm.connect();
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		roombacomm = new SerialWriter();
//		sndplayer= new SndPlayerMbed();
//        initialize();
        
        initdataDrivenController();
        sndplayer = new SndPlayer();
        
    }
    
    public static void main(String args[]) {
    	
        FindAPebble task = new FindAPebble();
        
//        task.roombaSpot(5);
   	 
//        roombacomm.pause(15000);
        
        
        //	
//        if (!task.connect())
//            return;
//        task.initialize();
        
        task.addWindowListener( new WindowAdapter() 
        {
            public void windowClosing( WindowEvent e )
            {
            	roombacomm.control();
            	roombacomm.stop();
                System.exit( 0 );
            }
        } );
        
        	 task.roombacomm.spin(10); //used as mark to notify user rommba start up
        	 
        	 
//        	 dataController.setFinalTargetPebbleId(3);
//        	 isRunning=true;	
        
    }
    
    public boolean connect() {
        if (!roombacomm.connect(roombacommPort)) {
            System.out.println("Couldn't connect to " + roombacommPort);
            return false;
        }
        System.out.println("succefully connected.");
        return true;
    }
    
    public void initialize() {
        
        System.out.println("Roomba startup");
        roombacomm.startup();
        // safemode
        roombacomm.control();
        roombacomm.pause(30);
        //roombacomm.send(new byte[]{(byte)148, (byte)1, (byte)7});
        roombacomm.startAutoUpdate();
        roombacomm.pause(30);
        roombacomm.playNote(72, 10); // C , test note
        roombacomm.pause(2000);
        System.out.println("Roomba setup finished");
//        System.out.println("roomba capacity: "+roombacomm.capacity());
//        System.out.println("roomba voltage: "+roombacomm.voltage());
//        System.out.println("roomba current: "+roombacomm.current());
        
    }
    
    @Override
    // AnglesEventListener
    public void anglesReady(AnglesEvent e) {
        FindAPebble.busyAnglesEventListener = true;
        int angles = e.getAngles();
        roombacomm.control();
        // roombacomm.spin(angle/10);
        // roombacomm.goForward(500); //mm
        // distance is increased so that, more communication
        // will be performed. And it will reduce the idle time.
        //public void smartCurveDrive(int angle,int distance,int speed )
//        roombacomm.smartCurveDrive(angle / 10, 550,130);
        
		roombacomm.control();
        roombacomm.smartCurveDrive(angles/10, 550,130);
        FindAPebble.busyAnglesEventListener = false;
    }
    
    @Override
    // StopRobotEventListener
    public void stopRobot() {
        busyStopEventListener = true;
        roombacomm.stop();
        busyStopEventListener = false;
    }
    
    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
        int id = e.getID();
        String keyString;
        if (id == KeyEvent.KEY_TYPED) {
            char c = e.getKeyChar();
            
            if(c=='+'){
            	FindAPebble.sndplayer.increaseVol();
            	return;
            }
            else if(c=='-'){
            	FindAPebble.sndplayer.decreaseVol();   
            	return;
            }
            else if(c=='0'){
            	dataController.resetRobot();
            	FindAPebble.roombacomm.stop();
            	return;
            }
           
            if(FindAPebble.isRunning){  	
            	FindAPebble.sndplayer.playWait(); 
            	return;
            }
            
            
            keyString = "key character = '" + c + "'";
            
            
            int temp= (int) c-48;
            System.out.println(temp);
            
            
            if(!FindAPebble.isRunning)
            {
                System.out.print("Input target Id::::::::::::::::::::::::::::::::::::: "+temp);	
                int targetId = temp;
                System.out.println(targetId);
                
                
               
                if(c=='*'){
                	dataController.locatePebbleNetwork();
                	return;	
                }
                
                
                if(targetId>9 || targetId<0)
                {
                	FindAPebble.roombacomm.playNote(50, 50); // C
					FindAPebble.roombacomm.pause(200);
					FindAPebble.roombacomm.playNote(32, 50); // G
					FindAPebble.roombacomm.pause(400);
					FindAPebble.roombacomm.playNote(50, 50); // C
					FindAPebble.roombacomm.pause(200);
					FindAPebble.roombacomm.playNote(32, 50); // G
					FindAPebble.roombacomm.pause(400);
                    return;
                    
                }
                FindAPebble.isRunning=true;
                
              
                dataController.setFinalTargetPebbleId(targetId);
            }
        } else {
            int keyCode = e.getKeyCode();
            keyString = "key code = " + keyCode
            + " ("
            + KeyEvent.getKeyText(keyCode)
            + ")";
        }
        	 System.out.println(keyString);
        
    }

    
}